using System;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Visuals.Fitness;
using ZoneFiveSoftware.Common.Data.Fitness;

namespace FilteredStatistics.View
{
    class FilteredStatisticsTrackSelectionInfo : IItemTrackSelectionInfo
    {
        public FilteredStatisticsTrackSelectionInfo(IActivity activity)
        {
            m_Activity = activity;
        }

#region IItemTrackSelectionInfo Members

        public string ItemReferenceId
        {
            get
            {
                if (m_Activity != null)
                {
                    return m_Activity.ReferenceId;
                }

                return string.Empty;
            }
        }

        public IValueRangeSeries<double> MarkedDistances
        {
            get { return null; }
        }

        public IValueRangeSeries<DateTime> MarkedTimes
        {
            get { return m_SelectedRanges; }
            set
            {
                ValueRangeSeries<DateTime> gpsTrackTimes = new ValueRangeSeries<DateTime>();

                if (m_Activity.GPSRoute != null)
                {
                    DateTime gpsStartTime = m_Activity.GPSRoute.StartTime;
                    DateTime gpsEndTime = m_Activity.GPSRoute.StartTime + new TimeSpan(0, 0, (int)m_Activity.GPSRoute.TotalElapsedSeconds);

                    // We can cause a crash in Track Coloring if we selected points outside the GPS track.  Make sure to
                    // clamp the selection, it'S probably good practice anyways.
                    foreach (ValueRange<DateTime> entry in value)
                    {
                        if (entry.Lower >= gpsStartTime && entry.Upper <= gpsEndTime)
                        {
                            gpsTrackTimes.Add(new ValueRange<DateTime>(entry.Lower,
                                                                       entry.Upper));
                        }
                        else if (entry.Lower < gpsStartTime && entry.Upper <= gpsEndTime)
                        {
                            gpsTrackTimes.Add(new ValueRange<DateTime>(gpsStartTime,
                                                                       entry.Upper));
                        }
                        else if (entry.Lower >= gpsStartTime && entry.Upper > gpsEndTime)
                        {
                            gpsTrackTimes.Add(new ValueRange<DateTime>(entry.Lower,
                                                                       gpsEndTime));
                        }
                    }
                }

                m_SelectedRanges = gpsTrackTimes;
            }
        }

        public IValueRange<double> SelectedDistance
        {
            get { return null; }
        }

        public IValueRange<DateTime> SelectedTime
        {
            get { return null; }
        }

#endregion

        private IActivity m_Activity = null;
        private IValueRangeSeries<DateTime> m_SelectedRanges = null;
    }
}
